<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class SongController extends MessageController {


    /**
     * 缓存到文件
     */
    private function cachesong(){

        $cache = S('cachesong');

        if (!empty($cache)){
            return $cache;
        }

        $info = M('song_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cachesong',$data,7200);

        return S('cachesong');

    }


    /**
     * 猜歌
     */
    public function sendsong(){

        exit;
//        $open = 'onZzl5XjyIO51zWOMXGyI9gUCf_s';
//        $where2['touser'] = $open;
//        $where2['status'] = '-1';
//        $info = M('song_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($where2)->limit(1)->order('wxopen_template_send_id desc')->select();

       $info = $this->cachesong();

        //$ranking = M('SongMember')->field('nickName,avatarUrl,score')->order('score desc')->find();

        $appid = "wxa55789390bbb94ea";
        $secret = "1f5f934b28f65f78137303c12eec48cc";

        $temp = "BF3fodXw_1XG0NF66ytvHfsAPFE44wk6GqA_OpHgG90";

        foreach ($info as $key=>$value){


            $fdata = array(
                'keyword1'=>array(
                    'value'=>'猜歌还有奖金！',
                    'color'=>'#ff0000',
                ),
                'keyword2'=>array(
                    'value'=>'全新猜歌模式，猜对了就有现金红包哦！',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'欢迎体验！',
                    'color'=>'#173177',
                ),

            );

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$temp,
                'page'=>'pages/index/index?appid=wx370386b7eb0c04bc&page=pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword1.DATA'
            );
            $res = $this->sendtemplate($appid,$secret,$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword2.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('song_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('song_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }
        echo 'ok';
        exit;
    }

}